package com.rd.student.jike.class2.lihongming.week731_806.Day0802;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

/**
 * @Auther:Heinrich
 * @Date:2023/8/2-08-02-21:26
 * @Description:com.rd.student.jike.class2.lihongming.Day0802
 * @version:1.0
 */
public class Question7 {
    public static void main(String[] args) throws InterruptedException, IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("控制台版cmd命令行竭诚为您服务：");
        Thread.sleep(800);
        System.out.println("温馨提示：输入dir查看当前路径下所有文件及文件夹");
        Thread.sleep(800);
        System.out.println("温馨提示：输入md创建一个文件夹");
        Thread.sleep(800);
        System.out.println("温馨提示：输入cd选择进入一个文件夹");
        Thread.sleep(800);
        System.out.println("温馨提示：输入copy将一个文件拷贝并创建一个新的文件");
        Thread.sleep(800);
        System.out.println("温馨提示：输入move将一个文件移动到另一个位置");
        Thread.sleep(800);
        System.out.println("温馨提示：输入end结束本次使用");
        while (true){
            String answer = scanner.nextLine();
            switch (answer){
                case "dir":
                    File file = new File(System.getProperty("user.dir"));
                    listAll(file);
                    break;
                case "md":
                    System.out.println("请输入创建文件夹的路径及文件夹名称：" );
                    createFile(scanner.nextLine());
                case "end":
                    System.out.println("感谢您的本次使用");
                    Thread.sleep(1500);
                    System.out.println("正在退出中请稍等");
                    Thread.sleep(500);
                    System.out.println("已退出");
                    System.exit(0);
                default:
                    System.out.println("指令错误核验指令！！！");
            }
        }
    }

    //查看路径下全部文件
    public static void listAll(File file) {
        File[] files = file.listFiles();
        if (files == null || files.length == 0) return;
        for (File tmpFile : files) {
            System.out.println(file.getName() + " : " + tmpFile.getName());
            if (tmpFile.isDirectory()) {
                listAll(tmpFile);
            }
        }
    }

    //按指定路径创建一个文件夹
    public static void createFile(String path) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    //拷贝一个文件并创建一个新的文件；
    public static void copyFile(String path1, String path2) throws IOException {
        File fileSource = new File(path1);
        File fileDestination = new File(path2);
        copyPaper(fileSource, fileDestination);
    }

    public static void copyFile(String path) throws IOException {
        File fileSource = new File(path);
        File fileDestination = new File(path + 1);
        copyPaper(fileSource, fileDestination);
    }

    public static void copyPaper(File fileSource, File fileDestination) throws IOException {
        FileInputStream is = new FileInputStream(fileSource);
        if (!fileDestination.exists()) {
            fileDestination.createNewFile();
        }
        FileOutputStream os = new FileOutputStream(fileDestination, true);
        byte[] pipe = new byte[1024];
        int len = 0;
        while ((len = is.read(pipe)) != -1) {
            os.write(pipe, 0, len);
        }
        is.close();
        os.close();
    }

    //将一个文件从一个位置移动到另一个位置
    public static void moveFile(String path1, String path2) throws IOException {
        copyFile(path1, path2);
        File fileDelete = new File(path1);
        fileDelete.delete();
    }
}
